import store from "@/vue-config/store";
import FrameConfig from "@/model/frame/FrameConfig";

export class PermissionTool {
  /**
   * 判断权限是否缓存在应用列表内,
   * 会用输入值与权限列表比较
   *
   * @param permission 要被判断的权限,
   *
   * @example
   * if(HasPermission("ROUTE:xxx:xxx"))
   */
  public static hasPermission(permission: string): boolean {
    const _permissions: string[] = store.get("app/permissions");
    console.log(permission);
    return _permissions.indexOf(permission) != -1;
  }

  public static getRoutePermissionCodes(
    routeNames: string[],
    appName = FrameConfig.APP_NAME
  ): string[] {
    return routeNames.map<string>(value =>
      this.getRoutePermissionCode(value, appName)
    );
  }

  public static getRoutePermissionCode(
    routeName: string,
    appName = FrameConfig.APP_NAME
  ): string {
    return `ROUTE:${appName}:${routeName}`;
  }

  public static getDirPermissionCodes(
    dirNames: string[],
    appName = FrameConfig.APP_NAME
  ): string[] {
    return dirNames.map<string>(value =>
      this.getDirPermissionCode(value, appName)
    );
  }

  public static getDirPermissionCode(
    dirName: string,
    appName = FrameConfig.APP_NAME
  ): string {
    return `DIR:${appName}:${dirName}`;
  }
}
